package com.ovhoo.vdm.vdm;





import java.util.ArrayList;



import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;

public class VDMListActivity extends ListActivity{
   
	
	private ProgressDialog waitingDialog;
	
	
	private VDMListAdapter listAdapter;
	

	private VDMControler vdmcontroler;
	
	
	Handler handler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.obj != null){
				ArrayList<VDMQuote> _quotes = (ArrayList<VDMQuote> ) msg.obj;
				listAdapter.setQuotes(_quotes);
				listAdapter.notifyDataSetChanged();
				waitingDialog.dismiss();
			}
			
		}
	};
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vdmlist);
        
        
        this.listAdapter = new VDMListAdapter(this);
        setListAdapter(this.listAdapter);
        
        
        //Fenetre d'attente
        this.waitingDialog = ProgressDialog.show(this, this.getResources().getString(R.string.vdm_progresdialog_title), this.getResources().getString(R.string.vdm_progresdialog_text));
        
        
        //Thread : controler
        this.vdmcontroler = VDMControler.getInstance();
    }
    
    public void onStart(){
    	super.onStart();
    	
    	
    	Bundle extras  = getIntent().getExtras();
        if(extras != null){
        	String paramIntent  = extras.getString("com.ovhoo.vdm.vdm.ShowAll"); 
        	
        	
        	if (paramIntent.equals("randomQuotes")){
        		this.setTitle("Quotes au hasard");
        		this.vdmcontroler.makeARequest( VDMDownloadRequest.requestType.LOAD_RANDOM_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}
        	else if (paramIntent.equals("topQuotes")){
        		this.setTitle("Top Quotes");
        		this.vdmcontroler.makeARequest( VDMDownloadRequest.requestType.LOAD_TOP_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}
        	else if (paramIntent.equals("flopQuotes")){
        		this.setTitle("Flop Quotes");
        		this.vdmcontroler.makeARequest( VDMDownloadRequest.requestType.LOAD_FLOP_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}
        	else{
        		this.setTitle("Dernieres Quotes");
        		this.vdmcontroler.makeARequest( VDMDownloadRequest.requestType.LOAD_LATEST_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}

        }
        else{
        	this.vdmcontroler.makeARequest( VDMDownloadRequest.requestType.LOAD_LATEST_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        }
    	
    	
    	
    }
    
    /* Creates the menu items */
    public boolean onCreateOptionsMenu(Menu menu) {
        //menu.add(0, 0, 0, "Menu 1");
        //menu.add(0, 1, 0, "Menu 2");
        return true;
    }

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 0:
            return true;
        case 1:
            return true;
        }
        return false;
    }


    

}